package com.project.web.config.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger2-api文档
 *
 * @author wyy
 * @date 2020-04-03
 */
@EnableSwagger2
@Configuration
public class Swagger2 {
    /**
     * 通过 createRestApi函数来构建一个DocketBean
     * 函数名,可以随意命名,喜欢什么命名就什么命名
     */
    @Bean
    public Docket createRestApi() {
        //添加head参数
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> parameters = new ArrayList<>();
        parameterBuilder.name("apiToken").description("API令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        parameters.add(parameterBuilder.build());
        parameterBuilder.name("apiId").description("API标识").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        parameters.add(parameterBuilder.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())//调用apiInfo方法,创建一个ApiInfo实例,里面是展示在文档页面信息内容
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.project.web.course.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(parameters);
    }

    /**
     * 构建 api文档的详细信息函数
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("课程服务API")
                .contact(new Contact("XXXXXXXX", "http://www.xxxxxxx.com", "wangyueyun0414@126.com"))
                .version("1.0.0")
                .description("一、接口调用规则如下" +
                        "\r\n 1: 接口域名：https://aip.jianxc.com" +
                        "\r\n 2: /course/auth/* header中参数为必填" +
                        "\r\n 3: /course/anon/* header中参数为非必填" +
                        "\r\n 4: /course/common/* header中参数为非必填" +
                        "\r\n 二、参数传递类型如下" +
                        "\r\n 1:【header】参数在request headers 里边提交" +
                        "\r\n 2:【form】以表单的形式提交仅POST方式" +
                        "\r\n 3:【query】get方式,参数完成自动映射赋值" +
                        "\r\n 4:【path】URL地址路径作为参数" +
                        "\r\n 5:【body】支持POST方式JSON提交参数")
                .build();
    }
}
